﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Application.Dtos;
using Microsoft.AspNetCore.Mvc;
using System.Linq.Dynamic.Core;
using Volo.Abp.Application.Services;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using Wz.Interface;
using Wz.Entities;
using Wz.Dto;

namespace Wz.Services
{
    public class MaterialsService: ApplicationService, IMaterialsService
    {
        private readonly IRepository<PurchaseMaterials, Guid> _purchases;
        public MaterialsService(IRepository<PurchaseMaterials, Guid> purchases)
        {
            _purchases = purchases;
        }
        
            /// <summary>
            /// //物资采购显示
            /// </summary>
            /// <param name="input"></param>
            /// <returns></returns>
            [HttpGet]
        [Route("/api/Materials_Show")]
        public async Task<PagedResultDto<Purchase_MaterialsDto>> MaterialsShow(PagedAndSortedResultRequestDto input)
        {
            try//异常处理
            {
                //创建查询
                var query = _purchases.OrderBy(input.Sorting);
                //获取总条数
                var totalCount = await query.CountAsync();

                //获取实体
                List<PurchaseMaterials> material_Purchasings = await query
                    .ToListAsync();

                //实体映射Dto
                List<Purchase_MaterialsDto> material_PurchasingDtos =
                    ObjectMapper.Map<List<PurchaseMaterials>, List<Purchase_MaterialsDto>>(material_Purchasings);

                //返回结果
                return new PagedResultDto<Purchase_MaterialsDto>(totalCount, material_PurchasingDtos);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 物资添加
        /// </summary>
        /// <param name="obj"></param>
        /// <redturns></returns>
        /// yyh
        public async Task<Purchase_MaterialsDto> Material_PurchasingAddAsync([FromForm]AddMaterial_PurchasingDto obj)
        {
            //赋值
            PurchaseMaterials item = new PurchaseMaterials(
                GuidGenerator.Create(),  //Guid
                DateTime.Now,    //当前时间
                obj.ApplyName,/// 创建人名称
                obj.Deleted,/// 否已删除
                obj.ProType,/// 类型
                obj.Material,/// 材质
                obj.Specs,/// 规格
                obj.Productions1,/// 国家
                obj.Productions2,/// 省
                obj.Num,/// 数量
                obj.DeliverTime,/// 期望交付日期
                obj.Info,/// 用途描述
                obj.Remarks,/// 备注
                obj.State,/// 审批状态
                obj.StateName,/// 审批人
                obj.ProName/// 采购货物名称
                );
            //调用添加方法
            var result = await  _purchases.InsertAsync(item);

            return ObjectMapper.Map<PurchaseMaterials, Purchase_MaterialsDto>(result);
        }
        /// <summary>
        /// 物资删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// 
        [HttpDelete]
        public async Task Material_PurchasingDeleteAsync(Guid id)
        {
            await _purchases.DeleteAsync(id);
        }
        /// <summary>
        /// 物资编辑
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        /// 
        [HttpPut]
        public async Task<Purchase_MaterialsDto> Material_PurchasingUptAsync([FromForm]UptPurchase_MaterialsDto obj)
        {
            //赋值
            PurchaseMaterials item = new PurchaseMaterials(
                obj.ID,  //Guid
                DateTime.Now,    //当前时间
                obj.ApplyName,/// 创建人名称
                obj.Deleted,/// 否已删除
                obj.ProType,/// 类型
                obj.Material,/// 材质
                obj.Specs,/// 规格
                obj.Productions1,/// 国家
                obj.Productions2,/// 省
                obj.Num,/// 数量
                obj.DeliverTime,/// 期望交付日期
                obj.Info,/// 用途描述
                obj.Remarks,/// 备注
                obj.State,/// 审批状态
                obj.StateName,/// 审批人
                obj.ProName/// 采购货物名称
                );
            //调用添加方法
            var result = await _purchases.UpdateAsync(item);

            return ObjectMapper.Map<PurchaseMaterials, Purchase_MaterialsDto>(result);
        }
        /// <summary>
        /// 物资反填信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// 
        [HttpGet]
        public async Task<Purchase_MaterialsDto> purchaseFind(Guid id)
        {
            var result = await _purchases.FindAsync(id);
            return ObjectMapper.Map<PurchaseMaterials, Purchase_MaterialsDto>(result);
        }
        #region 逻辑修改状态通过
        [HttpPost]
        public Purchase_MaterialsDto Material_PurchasingUpsize(Guid id)
        {
            PurchaseMaterials purchase = _purchases.FirstOrDefault(x => x.Id.Equals(id));
            if ((int)purchase.State == 1)
            {
                purchase.State = (Enum.AuditState)3;
                purchase.StateName = "王总";
            }
            else if ((int)purchase.State == 2)
            {
                purchase.State = (Enum.AuditState)3;
                purchase.StateName = "王总";
            }
            return ObjectMapper.Map<PurchaseMaterials, Purchase_MaterialsDto>(purchase);
        }
        #endregion
        #region 逻辑修改状态拒绝
        [HttpPost]
        public Purchase_MaterialsDto Material_PurchasingUpsizes(Guid id)
        {
            PurchaseMaterials purchase = _purchases.FirstOrDefault(x => x.Id.Equals(id));
            if ((int)purchase.State == 1)
            {
                purchase.State = (Enum.AuditState)4;
                purchase.StateName = "王总";
            }
            if ((int)purchase.State == 2)
            {
                purchase.State = (Enum.AuditState)4;
                purchase.StateName = "王总";
            }
            return ObjectMapper.Map<PurchaseMaterials, Purchase_MaterialsDto>(purchase);
        }
        #endregion
    }
}
